import html2canvas from 'html2canvas'

/*
* element  导出的元素
* filename 文件名
* ext      扩展文件名
*/
export function ExportImgElement(element, filename, ext) {
    html2canvas(element, {
        useCORS: true,
        allowTaint: false,
    }).then((canvas) => {
        if (navigator.msSaveBlob) {
            const blob = canvas.msToBlob() // IE10+
            return navigator.msSaveBlob(blob, name)
        } else {
            const imageurl = canvas.toDataURL('image/png')
            const aLink = document.createElement('a') // 创建a标签
            aLink.style.display = 'none'
            aLink.href = imageurl
            aLink.download = `${filename}.${ext}` // 下载文件名
            document.body.appendChild(aLink)
            aLink.click()
            document.body.removeChild(aLink) // 用完后移除元素
        }
    })
}

export function ExportImg(imgsrc) {
    var image = new Image()
    image.src = imgsrc
    // 解决跨域canvas污染问题
    // image.setAttribute('crossOrigin', 'anonymous')
    image.crossOrigin='anonymous';
    image.onload = function () {
        let canvas = document.createElement('canvas')
        canvas.width = image.width
        canvas.height = image.height
        let context = canvas.getContext('2d')
        context.drawImage(image, 0, 0, image.width, image.height)
        let url = canvas.toDataURL('image/png') // 得到图片的base64编码数据
        let a = document.createElement('a')
        a.download = 'download'
        a.href = url
        a.click()
    }
}